//
//  HSYBaseCardNavigationController+Animation.m
//  高仿暴走斗图
//
//  Created by key on 16/4/22.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "HSYBaseCardNavigationController+Animation.h"
#import "UINavigationController+Manager.h"
#import "UIImage+Canvas.h"
#import "HSYBaseNavigationController+CardAnimation.h"

@implementation HSYBaseCardNavigationController (Animation)

- (void)customCardPushViewController:(UIViewController *)viewController animated:(BOOL)animated completion:(void (^)(BOOL isAnimation))completion {

    if (animated) {
        
        UIImageView *trans = [self getScreenImageViewForFrame:(CGRect){0, 0, self.view.viewSize} andImage:[self.screenViewControllers objectAtIndex:self.viewControllers.count - 1]];//[self getScreenImageViewForFrame:(CGRect){0, 0, self.view.viewSize} captureImageInView:self.view];//先画出一张未push前的nav的效果图
        [self pushViewController:viewController animated:NO];
        
        UIImageView *card = [self getScreenImageViewForFrame:(CGRect){viewController.view.viewWidth, 0, self.view.viewSize} andImage:[self.screenViewControllers lastObject]];//[self getScreenImageViewForFrame:(CGRect){viewController.view.viewWidth , 0, self.view.viewSize} captureImageInView:self.view];//再画出此时push出来的页面
        
        [[UIScreen getKeyWindows] addSubview:card];//添加在windows层
        [self.view insertSubview:trans belowSubview:[UIScreen getKeyWindows]];//插入在nav的view层和window层之间
        
        CGAffineTransform transf = CGAffineTransformIdentity;
        @weakify(self);
        [UIView animateWithDuration:[self animationDuration] animations:^{
            
            @strongify(self);
            //动画内容为：向左平移效果图片，并且同一时间将nav的view层缩放
            card.frame = (CGRect){0, 0, self.view.viewSize};
            self.view.transform = CGAffineTransformScale(transf, 0.9f, 0.9f);
            
        } completion:^(BOOL finished) {
            
            @strongify(self);
            //动画结束后缩放nav为原来的比例，并且移除效果图卡
            [card removeFromSuperview];
            [trans removeFromSuperview];
            self.view.transform = CGAffineTransformScale(transf, 1.0f, 1.0f);
            completion(finished);
        }];
    } else {
        [self pushViewController:viewController animated:NO];
        completion(YES);
    }
}


- (void)customCardPOPViewControllerForAnimated:(BOOL)animated withCompletion:(void (^)(BOOL isAnimation))completion {
    
    if (animated) {
        
        UIImageView *top = [self getScreenImageViewForFrame:(CGRect){0, 0, self.view.viewSize} andImage:[self.screenViewControllers lastObject]];//[self getScreenImageViewForFrame:(CGRect){0, 0, self.view.viewSize} captureImageInView:self.view];
        [[UIScreen getKeyWindows] addSubview:top];//添加在windows层
        
        UIImageView *bottom = [self getScreenImageViewForFrame:(CGRect){0, 0, self.view.viewSize} andImage:[self.screenViewControllers objectAtIndex:self.viewControllers.count - 2]];
        
        CGAffineTransform transf = CGAffineTransformIdentity;
        self.view.transform = CGAffineTransformScale(transf, 0.9f, 0.9f);
        
        [self.view insertSubview:bottom belowSubview:[UIScreen getKeyWindows]];
        
        @weakify(self);
        [UIView animateWithDuration:[self animationDuration] animations:^{
            
            @strongify(self);
            top.frame = (CGRect){[UIScreen getKeyWindows].viewWidth, 0, [UIScreen getKeyWindows].viewSize};
            self.view.transform = CGAffineTransformScale(transf, 1.0f, 1.0f);
            
        } completion:^(BOOL finished) {
            
            @strongify(self);
            [top removeFromSuperview];
            [bottom removeFromSuperview];
            [self popViewControllerAnimated:NO];
            completion(finished);
        }];
        
    } else {
        
        [self popViewControllerAnimated:NO];
        completion(YES);
    }
    
}


@end
